package com.alander.nexusgo.itinerary.service.impl;

import com.alander.nexusgo.common.model.dto.ItineraryListDTO;
import com.alander.nexusgo.common.model.vo.ItineraryVO;
import com.alander.nexusgo.itinerary.mapper.ItineraryMapper;
import com.alander.nexusgo.itinerary.model.entity.Itinerary;
import com.alander.nexusgo.itinerary.service.ItineraryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author alander
*/
@Service
public class ItineraryServiceImpl extends ServiceImpl<ItineraryMapper, Itinerary>
    implements ItineraryService {

    @Override
    public ItineraryVO createItinerary(Itinerary itinerary, Long userId) {
        if(itinerary.getCooperate() == 2){

        }
        itinerary.setUserId(userId);
        this.save(itinerary);
        return convertToVO(itinerary);
    }

    @Override
    public ItineraryVO updateItinerary(Long id, Itinerary itinerary, Long userId) {
        Itinerary existing = this.getById(id);
        if (existing == null) {
            throw new RuntimeException("行程不存在");
        }

        if (!existing.getUserId().equals(userId)) {
            throw new RuntimeException("无权限修改他人行程");
        }

        itinerary.setId(id);
        this.updateById(itinerary);
        return convertToVO(this.getById(id));
    }

    @Override
    public boolean deleteItinerary(Long id, Long userId) {
        Itinerary existing = this.getById(id);
        if (existing == null) {
            throw new RuntimeException("行程不存在");
        }

        if (!existing.getUserId().equals(userId)) {
            throw new RuntimeException("无权限删除他人行程");
        }

        // 软删除
        LambdaUpdateWrapper<Itinerary> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Itinerary::getId, id)
                .set(Itinerary::getDelFlag, "1");
        return this.update(updateWrapper);
    }

    @Override
    public ItineraryVO getItineraryById(Long id) {
        Itinerary itinerary = this.getById(id);
        return itinerary != null ? convertToVO(itinerary) : null;
    }

    @Override
    public List<ItineraryVO> listItineraries(ItineraryListDTO itineraryListDTO) {
        LambdaQueryWrapper<Itinerary> queryWrapper = new LambdaQueryWrapper<>();

        if (itineraryListDTO.getUserId() != null) {
            queryWrapper.eq(Itinerary::getUserId, itineraryListDTO.getUserId());
        }

        if (itineraryListDTO.getTravelStyle() != null) {
            queryWrapper.eq(Itinerary::getTravelStyle, itineraryListDTO.getTravelStyle());
        }

        if (itineraryListDTO.getStatus() != null) {
            queryWrapper.eq(Itinerary::getStatus, itineraryListDTO.getStatus());
        }

        if (itineraryListDTO.getCooperate() != null) {
            queryWrapper.eq(Itinerary::getCooperate, itineraryListDTO.getCooperate());
        }

        if (itineraryListDTO.getAiGenerated() != null) {
            queryWrapper.eq(Itinerary::getAiGenerated, itineraryListDTO.getAiGenerated());
        }

        // 只查询未删除的行程
        queryWrapper.eq(Itinerary::getDelFlag, "0");

        queryWrapper.orderByDesc(Itinerary::getCreateTime);
        List<Itinerary> itineraryList = this.list(queryWrapper);

        return itineraryList.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    /**
     * 将Itinerary实体转换为ItineraryVO
     * @param itinerary Itinerary实体
     * @return ItineraryVO对象
     */
    private ItineraryVO convertToVO(Itinerary itinerary) {
        ItineraryVO itineraryVO = new ItineraryVO();
        BeanUtils.copyProperties(itinerary, itineraryVO);
        if (itinerary.getStartTime() != null) {
            itineraryVO.setStartTime(itinerary.getStartTime().toString());
        }
        if (itinerary.getEndTime() != null) {
            itineraryVO.setEndTime(itinerary.getEndTime().toString());
        }
        if (itinerary.getCreateTime() != null) {
            itineraryVO.setCreateTime(itinerary.getCreateTime().toString());
        }
        if (itinerary.getUpdateTime() != null) {
            itineraryVO.setUpdateTime(itinerary.getUpdateTime().toString());
        }
        return itineraryVO;
    }
}